/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package collectionutil;

import java.util.*;

/**
 *
 * @author NDA
 */
public class TestCollectionUtil {

    static Scanner input = new Scanner(System.in);
    CollectionUtil utilImplement = new UtilImplement();

    private List<Integer> generateList() {
        List<Integer> list = new ArrayList();
        int max = 30;
        Random random = new Random();
        for (int i = 0; i < max; i++) {
            list.add(random.nextInt(100));
        }
        return list;
    }

    private void testSort(List<Integer> arrayList) {

        Comparator<Integer> c = Collections.reverseOrder();
        utilImplement.sort(arrayList, c);
        System.out.println("Sorted List: ");
        System.out.println(arrayList);
        System.out.println("-------------------------------------------------");
    }

    private void testBinarySearch(List<Integer> arrayList) {
        Comparator<Integer> c = Collections.reverseOrder();
        System.out.println("Test method binarySearch");
        System.out.println("Comparator: Collections.reverseOrder");
        System.out.print("Enter Key: ");
        int key = input.nextInt();
        input.nextLine();
        int result = utilImplement.binarySearch(arrayList, key, c);
        int result1 = Collections.binarySearch(arrayList, key, c);
        System.out.println("Result of CollectionUtil.binarySearch() is " + result
                + ", and Collection.binarySearch() is " + result1);
        System.out.println("-------------------------------------------------");
    }

    private void testReverse(List<Integer> arrayList) {
        System.out.println("Test method reverse");
        System.out.println("Reversed list:");
        utilImplement.reverse(arrayList);
        System.out.println(arrayList);
        System.out.println("-------------------------------------------------");
    }

    private void testShuffle(List<Integer> arrayList) {
        System.out.println("Test method shuffle");
        System.out.println("Result of shuffle(List)");
        utilImplement.shuffle(arrayList);
        System.out.println(arrayList);
        Random random = new Random();
        System.out.println("Result of shuffle(List, random)");
        utilImplement.shuffle(arrayList, random);
        System.out.println(arrayList);
        System.out.println("-------------------------------------------------");
    }

    private void testSwap(List<Integer> arrayList) {
        System.out.println("Test method swap");
        System.out.println("Swap between element number 3 and 13");
        utilImplement.swap(arrayList, 3, 13);
        System.out.println(arrayList);
        System.out.println("-------------------------------------------------");
    }

    private void testFill(List<Integer> arrayList) {
        System.out.println("Test method fill");
        System.out.print("Enter new element: ");
        int ele = input.nextInt();
        input.nextLine();
        utilImplement.fill(arrayList, ele);
        System.out.println("New list: " + arrayList);
        System.out.println("-------------------------------------------------");
    }

    private void testCopy(List<Integer> list) {
        System.out.println("Test method copy");
        List<Integer> newlist = new ArrayList(list);
        Collections.copy(newlist, list);
        //utilImplement.copy(newlist, list);
        System.out.println("List is copied " + newlist);
        System.out.println("-------------------------------------------------");
    }

    private void testMin(List<Integer> list) {
        int min;
        Comparator<Integer> c = Collections.reverseOrder();
        System.out.println("Test method min, Comparator is Collections.reverseOrder");
        min = utilImplement.min(list, c);
        System.out.println("Min: " + min);
        System.out.println("-------------------------------------------------");

    }

    private void testMax(List<Integer> list) {
        int max;
        Comparator<Integer> c = Collections.reverseOrder();
        System.out.println("Test method min, Comparator is Collections.reverseOrder");
        max = utilImplement.max(list, c);
        System.out.println("Max: " + max);
        System.out.println("-------------------------------------------------");

    }

    private void testRotate(List<Integer> list) {
        System.out.println("Test method rotate");
        System.out.println("Rotate? ");
        int distance = input.nextInt();
        input.nextLine();
        utilImplement.rotate(list, distance);
        System.out.println("Rotated list: " + list);
        System.out.println("-------------------------------------------------");
    }

    private void testIndexOfSublist(List<Integer> list) {
        System.out.println("Test method indexOfSublist");
        List<Integer> subList = new ArrayList();
        System.out.println("Enter 5 element of Sublist:");
        for (int i = 0; i < 5; i++) {
            subList.add(input.nextInt());
            input.nextLine();
        }
        int index = utilImplement.indexOfSubList(list, subList);
        System.out.println("Index: " + index);
        System.out.println("-------------------------------------------------");
    }

    private void testLastIndexOfSublist(List<Integer> list) {
        System.out.println("Test method indexOfSublist");
        List<Integer> subList = new ArrayList();
        System.out.println("Enter 5 element of Sublist:");
        for (int i = 0; i < 5; i++) {
            subList.add(input.nextInt());
            input.nextLine();
        }
        int index = utilImplement.lastIndexOfSubList(list, subList);
        System.out.println("Index: " + index);
        System.out.println("-------------------------------------------------");
    }

    private void testNCopies() {
        System.out.println("Test method nCopies");
        System.out.print("n = ");
        int n = input.nextInt();
        input.nextLine();
        System.out.print("Element = ");
        int ele = input.nextInt();
        input.nextLine();
        List<Integer> demoList = utilImplement.nCopies(n, ele);
        System.out.println("List: " + demoList);
        System.out.println("-------------------------------------------------");
    }
    
    private void testFrequency(List<Integer> list){
        System.out.println("Test method frequency");
        System.out.print("Element = ");
        int ele = input.nextInt();
        input.nextLine();
        int freq = utilImplement.frequency(list, ele);
        System.out.println("Frequency = "+freq);
        System.out.println("-------------------------------------------------");
    }
    
    private void testDisjoint(){
        System.out.println("Test method disjoint");
        List<Integer> list1 = new ArrayList();
        System.out.println("Enter 5 element of list 1:");
        for (int i = 0; i < 5; i++) {
            list1.add(input.nextInt());
            input.nextLine();
        }
        List<Integer> list2 = new ArrayList();
        System.out.println("Enter 5 element of list 1:");
        for (int i = 0; i < 5; i++) {
            list2.add(input.nextInt());
            input.nextLine();
        }
        System.out.print("Disjoint? ");
        if (utilImplement.disjoint(list1, list2)) System.out.println("Yes");
        else System.out.println("No ");
        System.out.println("-------------------------------------------------");
    }
    
    private void testAddAll(){
        System.out.println("Test method addAll");
        List<Integer> list = new ArrayList(10);
        System.out.print("Enter Element 1: ");
        int ele1 = input.nextInt();
        input.nextLine();
        System.out.print("Enter Element 2: ");
        int ele2 = input.nextInt();
        input.nextLine();
        System.out.print("Enter Element 3: ");
        int ele3 = input.nextInt();
        input.nextLine();
        utilImplement.addAll(list, ele1, ele2, ele3);
        System.out.println("List "+list);
        System.out.println("-------------------------------------------------");
    }
    
    private void testRemoveAll(List<Integer> list){
        System.out.println("Test method removeAll");
        System.out.println("Old list: "+ list);
        System.out.print("Old element: ");
        int oldEle = input.nextInt();
        System.out.print("New element: ");
        int newEle = input.nextInt();
        input.nextLine();
        utilImplement.replaceAll(list, oldEle, newEle);
        System.out.println("New list: "+list);
        System.out.println("-------------------------------------------------");
    }

    public static void main(String[] args) throws Exception {
        List<Integer> list = new ArrayList();
        TestCollectionUtil test = new TestCollectionUtil();
        list = test.generateList();
        System.out.println("Original List:");
        System.out.println(list);
        //test method sort()
        test.testSort(list);
        //test method binarySearch()
        test.testBinarySearch(list);
        //test method min
        test.testMin(list);
        //test method max
        test.testMax(list);
        //test method frequency
        test.testFrequency(list);
        //test method indexOfSublist
        test.testIndexOfSublist(list);
        //test method lastIndexOfSublist
        test.testLastIndexOfSublist(list);
        //test method rotate
        test.testRotate(list);
        //test method reverse()
        test.testReverse(list);
        //test method shuffle()
        test.testShuffle(list);
        //test method swap()
        test.testSwap(list);
        //test method fill()
        test.testFill(list);
        //test method copy()
        test.testCopy(list);
        //test method addAll
        test.testRemoveAll(list);
        //test method nCopies
        test.testNCopies();
        //test method disJoint
        test.testDisjoint();
        //test method addAll
        test.testAddAll();
    }
}
